import { DetailsPage } from './../details/details';
import { ModalController } from 'ionic-angular/components/modal/modal-controller';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { RestProvider } from '../../providers/rest/rest';
import { BaseUI } from '../../common/baseui';

/**
 * Generated class for the DiscoveryPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-discovery',
  templateUrl: 'discovery.html',
})
export class DiscoveryPage extends BaseUI{

  questions: string[]
  errorMessage: any

  constructor(public navCtrl: NavController, 
    public modalCtrl: ModalController,
    public loadingCtrl: LoadingController,
    public rest: RestProvider,
    public navParams: NavParams) {
      super()
  }

  ionViewDidLoad(){
   this.getQuestions()
  }

  getQuestions () {
    let loading = super.showLoading(this.loadingCtrl, '加载中...')
    this.rest.getQuestions()
    .subscribe(f => {
      this.questions = f
      loading.dismiss() 
    }, error => {
      this.errorMessage = <any>error
    })
  }

  doRefresh(refresher) {
    this.getQuestions()
    // 关闭刷新
    refresher.complete()
  }

  gotoDetails(questionId) {
    // 页面之间的传参
    this.navCtrl.push(DetailsPage, {id: questionId})
  }
}
